package de.lusiardi.webdav.xml;

import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;

@XmlRootElement(name = "creationdate")
@XmlAccessorType(XmlAccessType.FIELD)
public class CreationDate {

    private static Logger logger = Logger.getLogger(CreationDate.class.getCanonicalName());
    /**
     * The date in ISO 8601 format as descriped in <ul>
     * <li>http://tools.ietf.org/html/rfc3339#section-5.6</li> </ul>
     */
    @XmlValue
    private String creationDate;

    public String getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(String creationDate) {
        this.creationDate = creationDate;
        if (!checkFormat()) {
            logger.warning(String.format("'%s' is an invalid ISO 8601 date.", creationDate));
        }
    }

    public boolean checkFormat() {
        Pattern datePattern = Pattern.compile("^\\d\\d\\d\\d-\\d\\d-\\d\\d[Tt]\\d\\d:\\d\\d:\\d\\d([Zz]|[+-]).*$");
        Matcher dateMatcher = datePattern.matcher(creationDate);

        if (!dateMatcher.matches()) {
            return false;
        }
        return true;
    }
}
